package com.sense.kukoo.biz_me.view

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.feel.common.global.bean.WebPage
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.databinding.BizMeActivitySettingsBinding
import com.sense.kukoo.biz_me.login.core.FacebookLoginManager
import com.sense.kukoo.biz_me.login.core.GoogleLoginManager
import com.sense.kukoo.biz_me.login.core.LineLoginManager
import com.sense.kukoo.biz_me.vm.SettingsViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.constant.ConstantConfig
import com.sense.kukoo.common.event.LogoutEvent
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.kv.login_history.LoginHistory
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.dialog.BottomConfigDialog
import com.sense.kukoo.common.wiget.dialog.BottomConfigDialog.BottomConfigListener
import com.sense.kukoo.common.wiget.dialog.MiddleConfigDialog
import com.sense.kukoo.lib_logger.LoggerManager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = ARouterUrl.ROUTE_ME_MODULE_SETTINGS_ACTIVITY)
class SettingsActivity : BaseMvvmActivity<SettingsViewModel, BizMeActivitySettingsBinding>() {

    private val TAG: String = javaClass.simpleName

    override fun getLayoutId(): Int = R.layout.biz_me_activity_settings

    override fun getViewModelClass(): Class<SettingsViewModel> = SettingsViewModel::class.java

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        mBinding.titleView.init(this)
        setSupportActionBar(mBinding.titleView)
        //
        bindData(mViewModel.data_lastLoginType){ lastLoginType ->
            LoggerManager.d("lastLoginType=${lastLoginType}")
            var type = ""
            when (lastLoginType) {
                LoginHistory.SOURCE_INNER -> {
                    type = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_type_hint_type_phone)
                }
                LoginHistory.SOURCE_FACEBOOK -> {
                    type = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_type_hint_type_facebook)
                }
                LoginHistory.SOURCE_GOOGLE -> {
                    type = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_type_hint_type_google)
                }
                LoginHistory.SOURCE_LINE -> {
                    type = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.login_type_hint_type_line)
                }
            }
            if(type.length > 0){
                mBinding.tvBtnLoggedInWith.setText(type)
            }
        }
        bindData(mViewModel.data_cacheSize){
            mBinding.tvBtnCache.setText(it)
        }
    }

    override fun initView() {
        mBinding.tvBtnCache.setOnClickListener(){
            showCleanCacheDialog()
        }
        mBinding.tvBtnManageAccount.setOnClickListener(){
            //
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_ME_MODULE_MANAGE_ACCOUNT_ACTIVITY)
                .navigation()
        }
        mBinding.tvBtnPrivacyPolicy.setOnClickListener(){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_LOGIN_MODULE_WEB_ACTIVITY)
                .withParcelable(ConstantConfig.ARG1, WebPage(url = ConstantConfig.URL.h5PrivacyPolicy()))
                .navigation()
        }
        mBinding.tvBtnTermsOfService.setOnClickListener(){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_LOGIN_MODULE_WEB_ACTIVITY)
                .withParcelable(ConstantConfig.ARG1, WebPage(url = ConstantConfig.URL.h5TermsOfService()))
                .navigation()
        }
        mBinding.tvBtnContentAgreement.setOnClickListener(){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_LOGIN_MODULE_WEB_ACTIVITY)
                .withParcelable(ConstantConfig.ARG1, WebPage(url = ConstantConfig.URL.h5UploadAgreement()))
                .navigation()
        }
        mBinding.tvBtnAbout.setOnClickListener(){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_ME_MODULE_ABOUT_ACTIVITY)
                .navigation()
        }
        mBinding.tvLogout.setOnClickListener() {
            showLogoutDialog()
        }

        //
        mViewModel.initData()
    }

    private fun showCleanCacheDialog(){
        val dialog = MiddleConfigDialog(this)
        val mListener = object : MiddleConfigDialog.MiddleConfigListener{
            override fun onLeftClick() {
                dialog.dismiss()
            }

            override fun onRightClick() {
                dialog.dismiss()
                mViewModel.onCleanCache()
            }
        }
        dialog
            .setTitleStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_title))
            .setMsgStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_msg))
            .setBtnLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_left_btn), getResources().getColor(com.sense.kukoo.common.R.color.dialog_btn_text_color_blue))
            .setBtnRight(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_right_btn), getResources().getColor(com.sense.kukoo.common.R.color.dialog_btn_text_color_red))
            .setListener(mListener)
            .show()
    }

    private fun showLogoutDialog(){
        val dialog = BottomConfigDialog(this)
        val listener: BottomConfigListener = object : BottomConfigListener {
            override fun onButtonDownClick() {
                dialog.dismiss()
            }

            override fun onButtonUpClick() {
                dialog.dismiss()
                EventBus.getDefault().post(LogoutEvent())
            }
        }
        dialog
            .setBtn_1(
                    com.sense.kukoo.common_i18n.R.string.my_subscriptions_cancel,
                resources.getColor(com.sense.kukoo.common.R.color.dialog_btn_text_color_red)
            )
            .setBtn_2(
                    com.sense.kukoo.common_i18n.R.string.settings_logout,
                resources.getColor(com.sense.kukoo.common.R.color.dialog_btn_text_color_blue)
            )
            .setHintMsg(com.sense.kukoo.common_i18n.R.string.settings_logout_hint_msg)
            .setListener(listener)
            .show()
    }

    override fun responseVM(action: Int) {}

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: LogoutEvent) {
        finish()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}